package com.atguigu.tingshu.user.api;

import cn.hutool.core.bean.BeanUtil;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.user.service.VipServiceConfigService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 用户信息api控制器
 *
 * @author ym
 * @date 2025/06/28 16:22
 */
@Tag(name = "用户管理接口")
@RestController
@RequestMapping("api/user")
@SuppressWarnings({"all"})
public class UserInfoApiController {

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private VipServiceConfigService vipServiceConfigService;

    /**
     * 获取用户信息vo
     *
     * @param userId 用户ID
     * @return {@link Result <UserInfoVo>}
     */
    @GetMapping("/userInfo/getUserInfoVo/{userId}")
    public Result<UserInfoVo> getUserInfoVo(@PathVariable Long userId) {
        UserInfo userInfo = userInfoService.getUserInfo(userId);
        UserInfoVo userInfoVo = BeanUtil.copyProperties(userInfo, UserInfoVo.class);
        return Result.ok(userInfoVo);

    }

    /**
     * getuser是付费轨道
     *
     * @param userId            用户ID
     * @param albumId           专辑ID
     * @param needcheckTrackIds 需要检查跟踪ID
     * @return {@link Result<Map<Long,Integer>>}
     */
    @PostMapping("/userInfo/userIsPaidTrack/{userId}/{albumId}")
    public Result<Map<Long, Integer>> getuserIsPaidTrack(@PathVariable Long userId,
                                                         @PathVariable Long albumId,
                                                         @RequestBody List<Long> needcheckTrackIds) {

        Map<Long, Integer> resultMap = userInfoService.userIsPaidTrack(userId, albumId, needcheckTrackIds);
        return Result.ok(resultMap);
    }

    /**
     * 找到所有
     *
     * @return {@link Result<List<VipServiceConfig>>}
     */
    @GetMapping("/vipServiceConfig/findAll")
    public Result<List<VipServiceConfig>> findAll() {
        return userInfoService.findAll();
    }

    @GetMapping("getVipServiceConfig/{id}")
    public Result<VipServiceConfig> getVipServiceConfig(@PathVariable Long id) {
        return Result.ok(vipServiceConfigService.getById(id));

    }

    @GuiGuLogin
    @GetMapping("/getalbumisPaidorNot/{albumId}")
    public Result<Boolean> getalbumisPaidorNot(
            @PathVariable Long albumId) {
        Long userId = AuthContextHolder.getUserId();
        Boolean b = userInfoService.getalbumisPaidorNot(userId, albumId);
        return Result.ok(b);
    }

    @GuiGuLogin
    @GetMapping("/getUserIsVipOrNot/{albumId}")
    public Result<Boolean> getUserIsVipOrNot(
            @PathVariable Long albumId) {
        Long userId = AuthContextHolder.getUserId();
        Boolean b = userInfoService.getUserIsVipOrNot(userId, albumId);
        return Result.ok(b);
    }

    @GuiGuLogin
    @PostMapping("/getuserIsPaidTrackIdList/{albumId}")
    public Result<List<Long>> getuserIsPaidTrackIdList(
            @PathVariable Long albumId, @RequestBody List<Long> needcheckIdList) {
        Long userId = AuthContextHolder.getUserId();
        List<Long> paidTrackIdList = userInfoService.getuserIsPaidTrackIdList(userId, albumId, needcheckIdList);
        return Result.ok(paidTrackIdList);
    }

    @GuiGuLogin
    @PostMapping("/addUserPaidRecord")
    public Result addUserPaidRecord(@RequestBody UserPaidRecordVo userPaidRecordVo) {
        Long userId = AuthContextHolder.getUserId();
        userInfoService.addUserPaidRecord(userId, userPaidRecordVo);
        return Result.ok();
    }

}



